package rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import Transaction.Transaction;


@SuppressWarnings("serial") 
public class RemoteConnectionImpl extends UnicastRemoteObject implements RemoteConnection {
	private Transaction tx;
	private String dbName ; 
	private boolean isTx = false;
	private boolean autoCommit = true;

	public RemoteConnectionImpl( String dbName ) throws RemoteException {
		tx = new Transaction();
		this.dbName = dbName ; 
	}

	public RemoteStatement createStatement() throws RemoteException {
		return  new RemoteStatementImpl(this)  ;
	}

	public void close() throws RemoteException {	
		try { 
			tx.commit() ;
			
		}catch ( Exception e ) {}
	}
	
// The following methods are used by the server-side classes.

	Transaction getTransaction() {  
		return tx;
	}
	
	public void commit() throws RemoteException {
		try {
			tx.commit();
			tx = new Transaction();
		}
		catch ( Exception e ) {
			throw new RemoteException ( "error occur when commit" ) ; 
		}
	}

	public void rollback() throws RemoteException {
		try {
			tx.rollback();
			tx = new Transaction();
		}catch ( Exception e ) { 
			throw new RemoteException ( "error occur when rollback" ) ; 
		}
	}

	public String getDbName() throws RemoteException {
		return dbName ;
	}
	
	public void setAutoCommit ( boolean b ) throws RemoteException {
		autoCommit = b ; 
		isTx = !b;
	}
	
	public boolean getAutoCommit () throws RemoteException {
		return autoCommit;  
	}

	public RemoteDbMeta getDBMeta() throws RemoteException {
		return new RemoteDbMetaImpl ( dbName ) ;
	}

}

